package com.ych.rabbitmq.listener;

import com.ych.SDK.alibaba.alipaySDK.entity.AlipayQueryOrderInfo_Request;
import com.ych.modules.common.service.UserAccountRechargeOrderService;
import com.ych.rabbitmq.exchange.UserRechargeOrderExchange;
import com.ych.redis.RedisLockUtil;
import com.ych.utils.UUID;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2021/3/11
 * Time: 14:17
 * Description:
 */
@Component
@RabbitListener(queues = UserRechargeOrderExchange.QUEUE_NAME)
@Slf4j
public class UserRechargeOrderExchangeListener {
    @Autowired
    private UserAccountRechargeOrderService userAccountRechargeOrderService;

    /**
     * 延时订单
     *
     * @param out_trade_no 订单号
     */
    @RabbitHandler
    public void orderDelayConsumer(String out_trade_no) {
        String lockName = "Lock_Order_" + out_trade_no;
        String lockId = UUID.randomUUID();
        if (RedisLockUtil.tryLock(lockName, lockId, 300)) {
            try {
                AlipayQueryOrderInfo_Request orderInfoRequest = new AlipayQueryOrderInfo_Request();
                orderInfoRequest.setOut_trade_no(out_trade_no);
                userAccountRechargeOrderService.dealOrderCallback(orderInfoRequest);
            } catch (Exception e) {
                log.error("延时订单错误信息{},订单号:{}", e.getMessage(), out_trade_no);
            } finally {
                RedisLockUtil.unlock(lockName, lockId);
            }
        }

    }
}
